package com.example.administrator.liveapp.activity;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.example.administrator.liveapp.R;
import com.example.administrator.liveapp.Xutils3.MyCallBack;
import com.example.administrator.liveapp.Xutils3.XUtils;
import com.example.administrator.liveapp.base.BaseRVAdapter;
import com.example.administrator.liveapp.base.BaseViewHolder;
import com.example.administrator.liveapp.bean.MallDetailsPageBean;
import com.example.administrator.liveapp.utils.AppUrl;
import com.example.administrator.liveapp.utils.TimeChangeUtil;
import com.example.administrator.liveapp.view.FullyLinearLayoutManager;
import com.google.gson.Gson;

import java.util.HashMap;
import java.util.Map;

/**
 * 回复商城留言列表activity
 */
public class Answering extends AppCompatActivity implements View.OnClickListener {

    private TextView shutdown;
    private RecyclerView consulting_list;
    private TextView number;
    private TextView book_name;
    private MallDetailsPageBean bean;
    private String bookId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_answering);
        initView();

    }

    private void initView() {
        shutdown = (TextView) findViewById(R.id.shutdown);
        consulting_list = (RecyclerView) findViewById(R.id.consulting_list);
        number = (TextView) findViewById(R.id.number);
        book_name = (TextView) findViewById(R.id.book_name);
        shutdown.setOnClickListener(this);
        initData();
    }

    private void initData() {
        Intent intent = getIntent();
        bookId = intent.getStringExtra("bookId");
        String url = AppUrl.URL + AppUrl.ShoppingDetailsUrl;
        Map<String, String> map = new HashMap<>();
        map.put("bookId", bookId);
        XUtils.Get(url, map, new MyCallBack<String>() {
            @Override
            public void onSuccess(String result) {
                super.onSuccess(result);
                Gson gson = new Gson();
                bean = gson.fromJson(result, MallDetailsPageBean.class);
                book_name.setText(bean.getBook().getBooktitle());
                if (bean.getBookreply() != null) {
                    number.setText("  的" + bean.getBookreply().size() + "个咨询");
                }

            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                super.onError(ex, isOnCallback);
            }

            @Override
            public void onFinished() {
                super.onFinished();
                FullyLinearLayoutManager linearLayoutManager = new FullyLinearLayoutManager(Answering.this);
                linearLayoutManager.setScrollEnabled(false);
                linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
                consulting_list.setLayoutManager(linearLayoutManager);
                consulting_list.setHasFixedSize(true);
                consulting_list.setNestedScrollingEnabled(false);
                BaseRVAdapter adapter = new BaseRVAdapter(Answering.this, bean.getBookreply()) {
                    @Override
                    public int getLayoutId(int viewType) {
                        return R.layout.consulting_item;
                    }

                    @Override
                    public void onBind(BaseViewHolder holder, int position) {
                        holder.setTextView(R.id.name, bean.getBookreply().get(position).getConsumerName());
                        holder.setTextView(R.id.questions_text, bean.getBookreply().get(position).getReplycomment());
                        holder.setTextView(R.id.reply_text, bean.getBookreply().get(position).getReplyanswer());
                        holder.setTextView(R.id.time, TimeChangeUtil.getStrTime5(bean.getBookreply().get(position).getReplytime()));
                    }

                };
                consulting_list.setAdapter(adapter);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.shutdown:
                finish();
                break;
        }
    }
}
